package org.btik.light.mobile.ui.devs.arm.handel;

import android.widget.SeekBar;
import org.btik.light.mobile.common.handler.SimpleCommand;
import org.btik.light.mobile.ui.devs.arm.model.ArmViewModel;
import org.btik.light.mobile.common.handler.CmdSender;

import java.util.ArrayList;

import static org.btik.light.mobile.ui.devs.arm.ArmMeta.*;


public class ArmPosHandler extends CmdSender implements SeekBar.OnSeekBarChangeListener {

    private final SimpleCommand armPosSetOpt;

    /**
     * 舵机索引
     */
    private final int index;

    private final ArmViewModel model;


    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        model.setPos(index, progress);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        int pos = model.pos(index, 50);
        ArrayList<Object> params = (ArrayList<Object>) armPosSetOpt.getParams();
        params.set(1, Integer.toString(pos));
        send(seekBar.getContext(), armPosSetOpt);
    }

    public ArmPosHandler(int index, ArmViewModel model) {
        choseDev = model.getDev();
        this.index = index;
        String indexStr = Integer.toString(index);
        this.armPosSetOpt = new SimpleCommand(OP_CODE_ARM_POS, indexStr, DEFAULT_SAFE_POS);
        this.model = model;
    }
}
